namespace Nice157Advices.Advices
{
    /// <summary>
    /// 42.使用泛型参数兼容泛型接口的不可变性
    /// </summary>
    public static class _42
    {
        /**
         * 让返回值类型返回比声明的类型派生程度更大的类型，即是“协变”
         *
         * 实际上，只要泛型类型参数在一个接口声明中不被用来作为方法的输入参数，我们都可以姑且把它看成是“返回值”类型的。
         */

        public static void Go ()
        {
            ISalary<Programmer> s = new BaseSalaryCounter<Programmer> ();
            PrintSalary (s);
        }

        // private static void PrintSalary (ISalary<Employee> s) => s.Pay ();
        private static void PrintSalary<T> (ISalary<T> s) => s.Pay ();

        private interface ISalary<T>
        {
            void Pay ();
        }

        private class BaseSalaryCounter<T> : ISalary<T>
        {
            public void Pay () => System.Console.WriteLine ("Pay base salary");
        }

        private class Employee
        {
            public string Name { get; set; }
        }

        private class Programmer : Employee { }
        private class Manager : Employee { }
    }
}